package com.example.demo.chat_room.server.handler;

import com.example.demo.chat_room.mongodb_domain.User;
import com.example.demo.chat_room.proto.ProtobufInfo;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Slf4j
@Service
// 直接给泛型
public class PBServerHandler extends SimpleChannelInboundHandler<ProtobufInfo.ClassInfo> {

    @Autowired
    private  MongoTemplate mongoTemplate;

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println(333);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println(444);
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, ProtobufInfo.ClassInfo classInfo) throws Exception {
        System.out.println(mongoTemplate.findAll(User.class));
        log.info("收到了");
        // 通过getDataEnum可以得到传来的是哪个类对象，用枚举类接就可以了
        ProtobufInfo.ClassInfo.ClassType dataEnum = classInfo.getDataEnum();
        // 判断一下是谁，直接用枚举类中的和传来的比较地址即可
        if(dataEnum == ProtobufInfo.ClassInfo.ClassType.User){
            // 然后就正常调它提供的get、set方法就行了
            ProtobufInfo.User user = classInfo.getUser();
            System.out.println(user.getName()+"++++"+user.getAge());
        } else if (dataEnum == ProtobufInfo.ClassInfo.ClassType.Game) {
            ProtobufInfo.Game game = classInfo.getGame();
            System.out.println(game.getPrice()+"++++"+game.getName());
        }
    }

}
